<?php
declare(strict_types=1);

namespace Jxzsxsp\WechatRobot\Markdown;

use Jxzsxsp\WechatRobot\Message;

class MarkdownMessage extends Message
{
    const MSG_TYPE = 'markdown';
    public $content = '';

    public function append($content, $color='')
    {
        if (!empty($color)) {
            $this->content .= "<font color=\"{$color}}\">{$content}</font>";
        } else {
            $this->content .= $content;
        }
    }

    /**
     * @param $title
     * @param int $level 1-6级标题
     */
    public function appendTitle($title, $level=1)
    {
        $this->content .= str_repeat('#', $level) . ' ' . $title . "\n\n";
    }

    public function appendBold($content)
    {
        $this->content .= "**{$content}**";
    }

    public function appendLink($url, $text)
    {
        $this->content .= "[{$text}]({$url})";
    }

    public function appendCode($code)
    {
        $this->content .= "`{$code}`";
    }

    public function appendReference($content)
    {
        $this->content .= "\n> {$content}";
    }

    /**
     * @param $content
     * @param string $color
     * info: 绿色
     * comment: 灰色
     * warning: 橙红色
     */
    public function appendColor($content, $color='')
    {
        if (!empty($color)) {
            $this->content .= "<font color=\"{$color}}\">{$content}</font>";
        } else {
            $this->content .= $content;
        }
    }

    public function appendEnter($num=1)
    {
        $this->content .= str_repeat("\n", $num);
    }

    protected function getMessage(): array
    {
        return [
            'msgtype' => self::MSG_TYPE,
            self::MSG_TYPE => [
                'content' => $this->content,
            ]
        ];
    }

}